﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using BrainTechLLC.WPF;
using System.Drawing;
using System.Windows.Media;
using BrainTechLLC;

namespace SimpleFileDataAndViewModels
{
    public class ImageProviderBase : GenericViewModelBase<ImageProviderBase, HasBytes>, INotifyPropertyChanged, IObjectLock, IImageSourceProvider, IBitmapProvider, IImageProviderBase 
    {
        protected object _lockObject = new object();
        object IObjectLock.LockObject { get { return _lockObject; } }
        protected Bitmap _Bitmap;
        public ImageSource _ImageSource;
        public int _ActualWidth;
        public int _ActualHeight;

        public ImageProviderBase ()
            : base()
        {
        }

        public ImageProviderBase(HasBytes fileImage)
            : base(fileImage)
        {
        }

        public HasBytes FileImage
        {
            get
            {
                return Item;
            }
        }

        public virtual Bitmap Bitmap
        {
            get
            {
                if (_Bitmap == null)
                {
                    lock (_lockObject)
                    {
                        if (_Bitmap == null)
                        {
                            byte[] bytes = _Item.Bytes;
                            if (bytes == null)
                            {
                                bytes = _Item.Load();
                                _Item.Bytes = bytes;
                            }

                            _Bitmap = bytes.ReadBitmapFromByteStream();
                            ActualWidth = _Bitmap.Width;
                            ActualHeight = _Bitmap.Height;
                        }
                    }
                }
                return _Bitmap;
            }
            protected set
            {
                if (_Bitmap != value)
                {
                    _Bitmap = value;
                    OnPropertyChanged("Bitmap");
                }
            }
        }

        public virtual ImageSource ImageSource
        {
            get
            {
                if (_ImageSource == null)
                {
                    lock (_lockObject)
                    {
                        if (_ImageSource == null)
                        {
                            Bitmap bitmap = _Bitmap;
                            if (bitmap != null)
                            {
                                _ImageSource = bitmap.LoadImageSource(Item.Bytes);
                            }
                            else
                            {
                                byte[] bytes = _Item.Bytes;
                                if (bytes == null)
                                {
                                    bytes = _Item.Load();
                                    _Item.Bytes = bytes;
                                }
                                _ImageSource = _Bitmap.LoadImageSource(bytes);
                            }
                            ActualWidth = (int)_ImageSource.Width;
                            ActualHeight = (int)_ImageSource.Height;
                        }
                    }
                }
                return _ImageSource;
            }
            protected set
            {
                if (_ImageSource != value)
                {
                    _ImageSource = value;
                    OnPropertyChanged("ImageSource");
                }
            }
        }

        public int ActualWidth
        {
            get { return _ActualWidth; }
            set
            {
                if (_ActualWidth != value)
                {
                    _ActualWidth = value;
                    OnPropertyChanged("ActualWidth");
                }
            }
        }

        public int ActualHeight
        {
            get { return _ActualHeight; }
            set
            {
                if (_ActualHeight != value)
                {
                    _ActualHeight = value;
                    OnPropertyChanged("ActualHeight");
                }
            }
        }

        public virtual void ClearAll()
        {
            ImageSource = null;
            Bitmap = null;            
            _Item.ClearAll();
        }
    }
}
